import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

public class Test {
	public static void main(String[] args) {
		Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8});
        //求集合元素只和
        Integer result = stream.reduce(0, Integer::sum);
        System.out.println(result);

      //求和
        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        stream.reduce((i, j) -> i + j).ifPresent(System.out::println);


        //求最大值
        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        stream.reduce(Integer::max).ifPresent(System.out::println);

        //求最小值
        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        stream.reduce(Integer::min).ifPresent(System.out::println);

        //做逻辑
        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        stream.reduce((i, j) -> i > j ? j : i).ifPresent(System.out::println);

        //求逻辑求乘机
        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        int result2 = stream.filter(i -> i % 2 == 0).reduce(1, (i, j) -> i * j);

        Optional.of(result2).ifPresent(System.out::println);
        
        
        //合并list
    	Map<Integer, List<String>> t=new HashMap<>();
    	t.put(1, Arrays.asList("a","b"));
    	t.put(2, Arrays.asList("c","d"));
    	t.put(3, Arrays.asList("e","f"));
        
        List<String> ts=t.values().stream().reduce(new ArrayList<>(), (i, j) -> {
			i.addAll(j);
			return i;
		});
        
        System.out.println(t);
        System.out.println(ts);
        
        
        System.out.println("==================================");
        Map<Integer, List<Double>> t1=new HashMap<>();
        t1.put(1, Arrays.asList(1d,2d));
        t1.put(2, Arrays.asList(3d,4d));
        t1.put(3, Arrays.asList(5d,6d));
        
        
       double a= t1.values().stream().mapToDouble(x->{
    	   return x.stream().reduce(0d, Double::sum);
       }).reduce(0, Double::sum);
        
       
       
        System.out.println(a);
        
        System.out.println("==================================");
        t.values().stream().reduce(new ArrayList<>(), (i, j) -> {
        	System.out.println(i);
        	System.out.println(j);
			return j;
		});
	}
}
